#include "view_time_20.h"
#include "view_time_19.h"
#include "view_time_20.inc"
#include "view_time_14.h"
#include "view_time_3.h"
#include "view_time_0.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#include "view_times.h"
#include "math.h"
#include "weather_ctrl.h"
#include "health_storage.h"
#include "app_trace.h"
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)
#ifdef TIME_20_SKIP
#else

static void show_view_time_show_temperature_float_with_font(int x, int y, float temperature, bool is_centigrade, bool is_center_align, const unsigned char *const *image_tab, int font_interval,
                                                            int point_interval, int unit_interval)
{
    Ensures(temperature >= 0);

    // const unsigned char *const *image_tab = num_16x18_TAB;
    int const font_width = image_tab[0][0] + (image_tab[0][1] << 8);
    int const font_height = image_tab[0][2] + (image_tab[0][3] << 8);
    int const interval = font_interval;
    int draw_x = 0;
    if (isnan(temperature))
    {
        if (is_center_align)
        {
            x += 26;
        }
        disp_draw_rect_color(x, y + font_height / 2 - 2, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        x += font_width + interval;
        disp_draw_rect_color(x, y + font_height / 2 - 2, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        return;
    }
    if (!is_centigrade)
    {
        temperature = 32.f + temperature * 1.8f;
    }
    // float interger_part = 0.f;
    // float decimal_part = 0.f;

    // decimal_part = modff(temperature, &interger_part);
    int temperature_int = temperature * 100;
    if (temperature < 0)
    {
        disp_draw_rect_color(x, y + font_height / 2 - 1, x + font_width - 1, y + font_height / 2, RGB565_COLOR_WHITE);
        x += font_width + interval;
    }
    draw_x = disp_draw_user_image_num_filter_color(x, y, temperature_int / 100, font_width + interval, image_tab);
    draw_x -= interval;
    draw_x += point_interval;
    disp_draw_rect_color(draw_x, y + font_height - 3, draw_x + 3 - 1, y + font_height - 1, RGB565_COLOR_WHITE);
    draw_x += 3 + point_interval;
    draw_x = disp_draw_user_image_num_with_digits_filter_color(draw_x, y, temperature_int % 100, 2, font_width + interval, image_tab);
    draw_x -= interval;
    draw_x += unit_interval;
    // draw_x += 6;
    if (is_centigrade)
    {
        disp_draw_user_image_filter_color(draw_x, y, img_time_19_tempearture_centigrade);
    }
    else
    {
        disp_draw_user_image_filter_color(draw_x, y, img_time_19_temperature_fahrenheit);
    }
}

static void view_time_20_back(void)
{
    uint8_t const img_time_20_back = FILE_IMG_time_20_back;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 0, 0, img_time_20_back);
}

static void view_time_20_temperature(void)
{
    float temperature = disp_ctrl.com_data.f_temp[26];
    show_view_time_show_temperature_float_with_font(92, 178, temperature, disp_ctrl.com_data.i_temp[21] == WEATHER_TEMPERATURE_TYPE_C, true, num_8x10_TAB, 1, 1, 1);
}

static void view_time_20_weather(void)
{
    int16_t min_weather_temperature = disp_ctrl.com_data.i_temp[19];
    int16_t max_weather_temperature = disp_ctrl.com_data.i_temp[20];

    if (disp_ctrl.com_data.i_temp[22] == 0)
    {
        disp_draw_rect_color(58, 131, 65, 132, RGB565_COLOR_WHITE);
        disp_draw_rect_color(68, 131, 76, 132, RGB565_COLOR_WHITE);
    }
    else
    {
        disp_draw_user_image_filter_color(61, 124, img_time_19_underline);

        if (min_weather_temperature < 0)
        {
            disp_draw_rect_color(40, 128, 44, 129, RGB565_COLOR_WHITE);
            min_weather_temperature = -min_weather_temperature;
        }

        disp_draw_user_image_filter_color(46, 124, num_8x10_TAB[min_weather_temperature / 10]);
        disp_draw_user_image_filter_color(53, 124, num_8x10_TAB[min_weather_temperature % 10]);

        int x = 66;
        if (max_weather_temperature < 0)
        {
            disp_draw_rect_color(x, 128, 69, 129, RGB565_COLOR_WHITE);
            max_weather_temperature = -max_weather_temperature;
            x += 5;
        }

        disp_draw_user_image_filter_color(x, 124, num_8x10_TAB[max_weather_temperature / 10]);
        x += 6;
        disp_draw_user_image_filter_color(x, 124, num_8x10_TAB[max_weather_temperature % 10]);
        x += 8;

        if (disp_ctrl.com_data.i_temp[21] != WEATHER_TEMPERATURE_TYPE_C)
        {
            disp_draw_user_image_filter_color(x, 124, img_time_19_temperature_fahrenheit);
        }
        else
        {
            disp_draw_user_image_filter_color(x, 124, img_time_19_tempearture_centigrade);
        }
    }

    uint8_t const *weather_icon = 0;
    if (disp_ctrl.com_data.i_temp[22] < 0)
    {
        weather_icon = img_time_20_weather_unknown;
    }
    else
    {
        switch (disp_ctrl.com_data.i_temp[18])
        {
        case WEATHER_TYPE_SUNSHINE: //??
            weather_icon = img_time_20_weather_sun;
            break;
        case WEATHER_TYPE_CLOUDY: //??
            weather_icon = img_time_20_weather_cloudy_day;
            break;
        case WEATHER_TYPE_RAINY: //??
            weather_icon = img_time_20_weather_rain;
            break;
        case WEATHER_TYPE_SNOW: //?
            weather_icon = img_time_20_weather_snowing;
            break;
        case WEATHER_TYPE_HAZE: //??
            weather_icon = img_time_20_weather_foggy_day;
            break;
        case WEATHER_TYPE_PARTLY_CLOUDY:
            weather_icon = img_time_20_weather_partly_cloudy;
            break;
        case WEATHER_TYPE_UNKNOW:
            weather_icon = img_time_20_weather_unknown;
            break;
        }
    }
    if (weather_icon != 0)
    {
        disp_draw_user_image_ignore_color(54, 99, RGB565_COLOR_BLACK, weather_icon);
    }
}

static void view_time_20_steps(void)
{
    int steps = disp_ctrl.com_data.i_temp[11];
    disp_draw_user_image_num_mid_gray_lucency(123, 75, RGB565_COLOR_WHITE, steps, 9, num_8x10_TAB);
}

static void view_time_20_heart_rate(void)
{
    int heart_rate = disp_ctrl.com_data.i_temp[17];
    if (heart_rate < 40)
    {
        disp_draw_rect_color(167, 130, 175, 131, RGB565_COLOR_WHITE);
        disp_draw_rect_color(177, 130, 185, 131, RGB565_COLOR_WHITE);
    }
    else
    {
        disp_draw_user_image_num_mid_gray_lucency(176, 125, RGB565_COLOR_WHITE, heart_rate, 9, num_8x10_TAB);
    }
}

static void view_time_20_time(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.com_data.i_temp[31] = (RTCGetSeconds() % 60) * 6;
        uint16_t temp16 = get_present_day_minute();
        disp_ctrl.com_data.i_temp[33] = (temp16 / 2) / 6 * 6 % 360;
        disp_ctrl.com_data.i_temp[32] = (temp16 * 6) / 6 * 6 % 360;
    }
    disp_draw_img_rotate(img_time_20_time_hour_hand, 0, 48, (120 << 16) | 120, disp_ctrl.com_data.i_temp[33]);
    disp_draw_img_rotate(img_time_20_time_minute_hand, 0, 82, (120 << 16) | 120, disp_ctrl.com_data.i_temp[32]);
    disp_draw_img_rotate(img_time_20_time_second_hand, 0, 87, (120 << 16) | 120, disp_ctrl.com_data.i_temp[31]);
}

void view_time_20_display(void)
{
    view_time_20_back();
    view_time_20_temperature();
    view_time_20_weather();
    view_time_20_heart_rate();
    view_time_20_steps();
    view_time_20_time();
}
#endif
